const { mongoose } = require("../util/mongoose");

let userSchema = mongoose.Schema({
    devName: { require: true, type: String },
    devNum: { required: true, type: String },
    devDate: { required: true, type: String },
    devSIM: String,
    devUse: Boolean,
    devLocation: Array,
    devAdmin: String,
    cTime:String
})
let Dev = mongoose.model("devs", userSchema);
//增加设备
const regist = (query = {}) => {
    let collection = new Dev(query);
    return collection.save()
        .then(res => res)
        .catch(err => {
            console.log(err);
            return false;
        })
}

//查找
const find = (query = {}) => {
    return Dev.find(query)
        .then(res => res)
        .catch(err => {
            console.log(err);
            return false;
        })
}

// 修改设备基本信息（devName,devNum,devDate不可更改） 通过devNum进行查找修改
const update = (query, updated) => {
    return Dev.updateOne(query, updated)
        .then(res => res)
        .catch(err => {
            console.log(err);
            return false
        })
}

//设备查询{空值查询所有设备，也可以通过devNum查询单个设备信息}
const findtodevNum = (query = {}, counts) => {
    return Dev.find(query).skip(counts.skip).limit(counts.limit)
        .then(res => res)
        .catch(err => {
            console.log(err);
            return false;
        })
}

//设备删除（通过传递devNum设备编号进行删除）
const del = (query) => {
    return Dev.deleteOne(query)
        .then(res => res)
        .catch(err => {
            console.log(err)
            return false
        })
}

//查询所有设备
//设备查询{空值查询所有设备，也可以通过devNum查询单个设备信息}
const findAlldev = (query = {}) => {
    return Dev.find(query)
        .then(res => res)
        .catch(err => {
            console.log(err);
            return false;
        })
}

module.exports = {
    regist, find, update, findtodevNum, del,findAlldev
}